﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapInfo.MiPro.Interop;
using XtremeDockingPane;
using AxXtremeDockingPane;
using System.Windows.Forms;

namespace MapWindowDocking
{
    public class WindowDockManger
    {
        private WininfoControl windowinfocontrol;
        private LegendControl legendControl;

        private readonly MapinfoWrapper mapinfo;

        public WindowDockManger(MapinfoWrapper mapinfoSession, Window  mapwindow)
        {
            this.mapinfo = mapinfoSession;
            this.MapWindow = mapwindow;
        }

        public Window MapWindow { get; set; }

        private AxDockingPane innerdockingmanager;
        private AxDockingPane InnerDockingManger
        {
            get 
            {
                if (this.innerdockingmanager == null)
                {
                    this.innerdockingmanager = new AxDockingPane();
                    this.innerdockingmanager.CreateControl();
                    this.innerdockingmanager.AttachToWindow(this.MapWindow.Handle.ToInt32());
                    this.innerdockingmanager.VisualTheme = VisualTheme.ThemeWord2007;
                    this.innerdockingmanager.Options.AlphaDockingContext = true;
                    this.innerdockingmanager.Options.ShowDockingContextStickers = true;
                    this.innerdockingmanager.Options.StickerStyle = DockingContextStickerStyle.StickerStyleVisualStudio2005;
                }
                return this.innerdockingmanager;
            }
        }

        Pane legendPane;
        public void CreateLegendPanel()
        {
            legendControl = new LegendControl(this.mapinfo);
            legendControl.AssociatedWindow = this.MapWindow;

            legendPane = InnerDockingManger.CreatePane(1, 200, 200, DockingDirection.DockRightOf);
            legendPane.Hidden = true;
            legendPane.Handle = legendControl.Handle.ToInt32();
            legendPane.Options = PaneOptions.PaneNoCloseable;
            legendPane.Title = "Legend";
        }

        Pane pane;
        public void CreateWindowInfoPanel()
        {
            windowinfocontrol = new WininfoControl(this.mapinfo, this.MapWindow);

            this.mapinfo.SessionCoordSys = this.MapWindow.CoordSys;

            this.windowinfocontrol.UpdateControlsWithValues();

            pane = InnerDockingManger.CreatePane(2, 50, 50, DockingDirection.DockBottomOf);
            pane.Handle = windowinfocontrol.Handle.ToInt32();
            pane.Options = PaneOptions.PaneNoCloseable;
            pane.Title = "Window Info";
        }

        public void Unload()
        {
            //this.InnerDockingManger.DestroyPane(legendPane);
            this.innerdockingmanager.DestroyPane(pane);
        }
    }
}
